package com.gjintel.framework;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.gjintel.auction.business.model.AuctionInfo;
import com.gjintel.auction.business.service.IAuctionService;
import com.gjintel.framework.constant.ProjectStatusAuction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.gjintel.business.proj.model.ComposeIdPK;
import com.gjintel.business.proj.model.ComposeIdPK2;
import com.gjintel.business.proj.model.DepositReturn;
import com.gjintel.business.proj.model.DepositReturnOut;
import com.gjintel.business.proj.model.ProjectBidInfo;
import com.gjintel.business.proj.model.ProjectInfo;
import com.gjintel.business.proj.service.IDepositReturnService;
import com.gjintel.business.proj.service.IProjectBidService;
import com.gjintel.business.proj.service.IProjectInfoService;
import com.gjintel.framework.common.service.IDepositExecuteService;
import com.gjintel.framework.common.service.IDepositService;
import com.gjintel.framework.constant.Constant;
import com.gjintel.framework.constant.ProjectStatus;
import com.gjintel.framework.log.model.LogInfo;
import com.gjintel.framework.log.util.LogUtil;
import com.gjintel.framework.log.util.LogUtil.LogType;
import com.gjintel.framework.register.model.RegisterUser;
import com.gjintel.framework.register.service.IRegisterUserService;
import com.gjintel.framework.util.dto.Bank;
import com.gjintel.sdk.util.StringUtil;

@Component
public class QuartzAll {
	
	@Value("${cron.bank.enable}")
	private String bankEnable;
	
	@Autowired
	private IDepositService depositService;
	
	@Autowired
	private IProjectInfoService projectInfoService;
	
	@Autowired
	private IDepositReturnService depositReturnService;
	
	@Autowired
	private IRegisterUserService registerUserService;
	
	@Autowired
	private IProjectBidService projectBidService;
	
	@Autowired
	private IDepositExecuteService depositExecuteService;

	@Autowired
	private IAuctionService auctionService;
	
	public QuartzAll(){
        System.out.println("Quartz创建成功");
    }
	
	 //查询投标保证金退款：取出depositFlag为false的项目，
	//发送请求到银行，查询到商品信息，取出并保存到depositeReturn表中
    @Scheduled(cron = "${cron.bank.retrieve.deposite}")
    public void retrieveBankData(){
    	if(!bankEnable.equals("1")){
    		System.out.println("禁止bank retrieve!");
    		return;
    	}
    	
    	System.out.println("QuartzBank执行的任务调度---查询投标保证金退款");
        //调接口文件 获取已退回保证金用户并保存到deposieReturn
    	
    	AuctionInfo pj= new AuctionInfo();
    	pj.setReturnDepositFlag(false);
//		pj.setAuditable(null);
    	List<AuctionInfo>  projs=auctionService.findAllByIds(null,pj,null,null,null,null,null,null,null,null,null,null);
    	for(AuctionInfo projectInfo:projs){
    		if( projectInfo.getBankAuthCode()==null || projectInfo.getBankAuthCode().equals("")||projectInfo.getCode()==null){
    			continue;   //不符合条件，不需要写值，直接跳过
    		}
    		
    		List<Bank> banks =this.depositService.searchReturnDepositByProj(projectInfo.getId(),projectInfo.getAuctionName());
    		if (banks==null || banks.size()==0){
    			continue;   //不符合条件，不需要写值，直接跳过
    		}
    		for(Bank b:banks){
    			
    			if(b.getInName()==null || b.getInName().equals("")){
    				
    				continue;  //不符合条件，不需要写值，直接跳过
    			}
    			//保存已退还的保证金
    			
    			
    			ComposeIdPK composeIdPK = new ComposeIdPK();
    			composeIdPK.setCompanyName(b.getInName());
    			composeIdPK.setProjectId(projectInfo.getId());
    			composeIdPK.setDepositType(Constant.DEPOSIT_TYPE_BIDDER);    
    			composeIdPK.setHstSeqNum(b.getHstSeqNum());//交易流水号
    			//TODO 增加composeIdPk的另外一个键：银行流水号
    			DepositReturn _dr = depositReturnService.getDepositListByHstSeqNum(b.getHstSeqNum());
				//如果数据库里面取出的保证金对象主键中的单位名称与银行返回的InName 不一致，则删除现有的保证金对象，重新写入保证金对象
				if(_dr != null&&!_dr.getPk().getCompanyName().equals(b.getInName())){
                      _dr=depositReturnService.updateDepositReturnPK(_dr,composeIdPK);
				}
    			
    			//DepositReturn _dr = depositReturnService.get(DepositReturn.class, composeIdPK);
    			RegisterUser reg =null;
    			if (_dr==null ){
    				_dr = new DepositReturn();
    			}
				  List<RegisterUser> lst =registerUserService.getSameAccountUser(b.getInAcct());
    			if(lst==null||lst.size()<=0){
    				//如果没有发现这个用户，调用系统外用户
    				this.saveOutReturn(b, projectInfo,Constant.DEPOSIT_TYPE_BIDDER);
    				continue;
    			}else{
    				reg=lst.get(0);
				}

    			if(reg!=null && b.getInAcct().equals(reg.getAccount())){
    				_dr.setBidderId(reg.getId());
    				_dr.setPk(composeIdPK);
        			_dr.setInAcct(b.getInAcct());
        			_dr.setTnAmount(new BigDecimal(b.getInAmount()));
            		_dr.setBiaoDuanNo(projectInfo.getId());
            		_dr.setUpdateTime(new Date());
            		_dr.setProjectCode(projectInfo.getCode());
            		_dr.setProjectType(projectInfo.getCatagoryName());

            		//银行接口result:  1: 初始化接收      2: 退款失败           3: 退款成功
            		//修改：0：不成功， 1：成功，TODO:结合AddWord来判断细节如退款失败
            		switch(b.getResult()){
            		case "0":   //1
            			if(_dr.getStatus()!=null&&_dr.getStatus()!=Constant.DEPOSIT_RETURN){
            				_dr.setStatus(Constant.DEPOSIT_UN_RETURN);
            			}
            			break;
//            		case "2":   //2: 退款失败
//            			_dr.setStatus(Constant.FAILE_RETURN);
//            			break;
            		case "1":
            			_dr.setStatus(Constant.DEPOSIT_RETURN);
            			String date ="";
            	    	SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyyMMddhhmmss");
            			date = b.getInDate() + b.getInTime(); // 拼接日期
            			try {
            				// 退款时间
                			_dr.setReturnTime(simpleDateFormat.parse(date));
            			} catch (ParseException e) {
            				e.printStackTrace();
            			}
            			break;
            		}
            		
            		_dr.setAddWord(b.getAddWord());
            		
            		depositReturnService.saveOrUpdate(_dr);
    			}else{
    				//TODO：将该用户录入到非系统外用户中
    				this.saveOutReturn(b, projectInfo,Constant.DEPOSIT_TYPE_BIDDER);
    			}
    		}
    	}
    }
    
    /**
     * 更新系统外用户退款信息
     * @param bank
     */
    private void saveOutReturn(Bank bank,AuctionInfo proj,int depositType){
    	//组装
    	ComposeIdPK2 pk= new ComposeIdPK2();
    	pk.setCompanyName(bank.getInName());
    	pk.setHstSeqNum(bank.getHstSeqNum());
    	pk.setProjectId(proj.getId());
    	pk.setDepositType(depositType);//保证金类型
    	//查询数据库中是否有此信息
    	DepositReturnOut depositReturn=depositReturnService.get(DepositReturnOut.class, pk);
    	if(depositReturn!=null){
	    	//具体业务逻辑
			depositReturn.setAddWord(bank.getAddWord());
			//银行接口result:  1: 初始化接收      2: 退款失败           3: 退款成功
			//修改：0：不成功， 1：成功，TODO:结合AddWord来判断细节如退款失败
			switch(bank.getResult()){
			case "0":   //1
				if(depositReturn.getStatus()!=null&&depositReturn.getStatus()!=Constant.DEPOSIT_RETURN){
					depositReturn.setStatus(Constant.DEPOSIT_UN_RETURN);
				}
				break;
			case "1":
				depositReturn.setStatus(Constant.DEPOSIT_RETURN);
				try {
					//退款时间
					String date ="";
			    	SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyyMMddhhmmss");
					date = bank.getInDate() + bank.getInTime(); // 拼接日期
					// 退款时间
					depositReturn.setReturnTime(simpleDateFormat.parse(date));
				} catch (ParseException e) {
					e.printStackTrace();
				}
				break;
			}
			depositReturnService.saveOrUpdate(depositReturn);
    	}
		
    }
    
    
    
    //查询投标保证金：查询等待开标的项目，并将其
    @Scheduled(cron = "${cron.bank.retrieve.depositeBid}")
    public void retrieveBankDataOfBid() throws Exception{
    	if(!bankEnable.equals("1")){
    		System.out.println("禁止bank retrieve!");
    		return;
    	}
    	
    	System.out.println("QuartzBank执行查询投标保证金 任务调度--查询投标保证金");
    	
    	String date ="";
    	SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyyMMddhhmmss");
    	List<AuctionInfo>  projs=auctionService.findAllByStatus(null, null, getBidProjectStatus());//查询待开标状态下的项目
		for (AuctionInfo pi : projs) {
			if( pi.getBankAccount()==null || pi.getBankAuthCode().equals("")){
				continue;   //不符合条件，不需要写值，直接跳过
			}
			List<Bank> bankreturnList = depositService.getDepositList(pi.getId());
			for (Bank bank : bankreturnList) {
				if (bank==null || bank.getInName()==null||"".equals(bank.getInName())) continue;
				System.out.println("QuartzAll.retrieveBankDataOfBid()---------->bank.getInName(): "+bank.getInName());
				RegisterUser rUser =null;
				List<RegisterUser> lst =registerUserService.getSameAccountUser(bank.getInAcct());
				if(lst.size()>0){
                   rUser=lst.get(0);
				}
				if (rUser==null){//系统无此账号
			        if(bank.getInName()!=null&&bank.getInAmount()!=null){
			        	//未查到相关数据，系统记录与银行数据不一致，记录系统日志，并忽略这条记录
				        LogInfo log1= new LogInfo("从银行返回的数据中查找不到用户信息"+bank.getInName()+"【"+bank.getInAmount()+"】","","",
				        		String.valueOf(LogType.ADD.ordinal()), "QuartZAll.class", "");
				        LogUtil.addLog(log1);
				        //如果没有发现这个用户，调用系统外用户
	    				this.saveOutDeposit(bank, pi,Constant.DEPOSIT_TYPE_BIDDER);
			        }
					continue;
				}
				if(rUser!=null && !bank.getInAcct().equals(rUser.getAccount())){//系统账号的银行卡号和bank的卡号不一致
					//未查到相关数据，系统记录与银行数据不一致，记录系统日志，并忽略这条记录
			        LogInfo log1= new LogInfo("从银行返回的数据中的银行卡号和注册账号卡号不一致： "+bank.getInName(),"","",
			        		String.valueOf(LogType.ADD.ordinal()), "QuartZAll.class", "");
			        LogUtil.addLog(log1);
    				//如果发现这个用户的系统账号的银行卡号和bank的卡号不一致，调用系统外用户
    				this.saveOutDeposit(bank, pi,Constant.DEPOSIT_TYPE_BIDDER);
			        
					continue;
				}
				//查询系统外信息中是否查询到此数据
				updateOutDepositReturn(bank,pi,rUser,Constant.DEPOSIT_TYPE_BIDDER);
				ComposeIdPK composeIdPK = new ComposeIdPK();
    			composeIdPK.setCompanyName(bank.getInName());
    			composeIdPK.setProjectId(pi.getId());
    			composeIdPK.setDepositType(Constant.DEPOSIT_TYPE_BIDDER);
    			composeIdPK.setHstSeqNum(bank.getHstSeqNum());//交易流水号
    			//DepositReturn depositReturn = depositReturnService.get(DepositReturn.class, composeIdPK);
				DepositReturn depositReturn = depositReturnService.getDepositListByHstSeqNum(bank.getHstSeqNum());
				//如果数据库里面取出的保证金对象主键中的单位名称与银行返回的InName 不一致，则删除现有的保证金对象，重新写入保证金对象
				if(depositReturn != null&&!depositReturn.getPk().getCompanyName().equals(bank.getInName())){
                      depositReturn=depositReturnService.updateDepositReturnPK(depositReturn,composeIdPK);
				}
    			if (depositReturn==null ){
    				depositReturn = new DepositReturn();
    				depositReturn.setBankName(rUser.getBank());
    				depositReturn.setProjectType(pi.getCatagoryName());
    				depositReturn.setPk(composeIdPK);
    				depositReturn.setBidderId(rUser.getId());
    				depositReturn.setBiaoDuanNo(pi.getId());// 标段号
    				depositReturn.setProjectName(pi.getAuctionName());
    				depositReturn.setReturnOpt(0);// 默认为未提交申请
    				depositReturn.setStatus(0);// 默认为未退还
    				depositReturn.setProjectCode(pi.getCode());
    				
    				date = bank.getInDate() + bank.getInTime(); // 拼接日期
    				depositReturn.setTransTime(simpleDateFormat.parse(date));// 时间
    				//depositReturn.setHstSeqNum(bank.getHstSeqNum());
    				depositReturn.setInAcct(bank.getInAcct());
    				depositReturn.setIsBid(false);	// 默认为未投标
    			}else{
//    				//查找是否投标，如果已投标，则更新
//    				for (ProjectBidInfo pb : projectBidInfoList) {
//    					if (bank.getInName().equals(pb.getBidderName())) {
//    						depositReturn.setBid(true);// 设置已投标
//    						break;
//    					}
//    				}
    			}
    			depositReturn.setTnAmount(new BigDecimal(bank.getInAmount()));// 金额再次更新
				this.projectBidService.saveOrUpdate(depositReturn);
			}
		}
    }

    /**
     * 查询系统外信息中是否查询到此数据
     * @param bank
     * @param pi
     */
    private void updateOutDepositReturn(Bank bank, AuctionInfo pi,RegisterUser rUser,int depositType) {
    	//组装
    	ComposeIdPK2 pk= new ComposeIdPK2();
    	pk.setCompanyName(bank.getInName());
    	pk.setHstSeqNum(bank.getHstSeqNum());
    	pk.setProjectId(pi.getId());
    	//查询数据库中是否有此信息,如果有就删除
    	DepositReturnOut depositReturnOut=depositReturnService.get(DepositReturnOut.class, pk);
    	if(depositReturnOut!=null){
	    	//将此数据复制到系统内保证金中去
	    	ComposeIdPK inPk= new ComposeIdPK();
	    	inPk.setCompanyName(bank.getInName());
	    	inPk.setHstSeqNum(bank.getHstSeqNum());
	    	inPk.setProjectId(pi.getId());
	    	inPk.setDepositType(Constant.DEPOSIT_TYPE_BIDDER);
	    	DepositReturn depositReturnIn = depositReturnService.get(DepositReturn.class, inPk);
	    	if(depositReturnIn==null){
	    		depositReturnIn=new DepositReturn();
	    		depositReturnIn.setBankName(rUser.getBank());
				depositReturnIn.setProjectType(pi.getCatagoryName());
				depositReturnIn.setPk(inPk);
				depositReturnIn.setBidderId(rUser.getId());
				depositReturnIn.setBiaoDuanNo(pi.getId());// 标段号
				depositReturnIn.setProjectName(pi.getAuctionName());
				depositReturnIn.setReturnOpt(depositReturnOut.getReturnOpt());
				depositReturnIn.setStatus(depositReturnOut.getStatus());
				depositReturnIn.setProjectCode(pi.getCode());
				depositReturnIn.setTransTime(depositReturnOut.getTransTime());// 时间
				depositReturnIn.setInAcct(bank.getInAcct());
				depositReturnIn.setReturnTime(depositReturnOut.getReturnTime());
				depositReturnIn.setIsBid(false);	// 默认为未投标
				depositReturnIn.setTnAmount(depositReturnOut.getTnAmount());
				depositReturnIn.setUpdateTime(depositReturnOut.getUpdateTime());
	    	}
	    	this.depositReturnService.removeById(DepositReturnOut.class, pk);//删除此系统外保证金数据
    	}
	}

	/**
     * 保存系统外用户保证金信息
     */
    private void saveOutDeposit(Bank bank,AuctionInfo proj,int depositType){
    	//组装
    	ComposeIdPK2 pk= new ComposeIdPK2();
    	pk.setCompanyName(bank.getInName());
    	pk.setHstSeqNum(bank.getHstSeqNum());
    	pk.setProjectId(proj.getId());
    	pk.setDepositType(depositType);//保证金类型
    	
    	//查询数据库中是否有此信息
    	DepositReturnOut depositReturn=depositReturnService.get(DepositReturnOut.class, pk);
    	if(depositReturn==null){
    		depositReturn= new DepositReturnOut();
    		depositReturn.setReturnOpt(0);// 默认为未提交申请
    		depositReturn.setStatus(0);// 默认为未退还
    	}
    	
    	depositReturn.setPk(pk);
    	//TODO :具体业务逻辑
    	depositReturn.setProjectType(proj.getCatagoryName());
    	
    	depositReturn.setBiaoDuanNo(proj.getId());// 标段号
		depositReturn.setProjectName(proj.getAuctionName());
		depositReturn.setProjectCode(proj.getCode());
		if("".equals(bank.getInAmount())){
			depositReturn.setTnAmount(new BigDecimal("0"));
		}else{
			depositReturn.setTnAmount(new BigDecimal(bank.getInAmount()));
		}
		
		depositReturn.setBankName(bank.getInName());
		String date ="";
    	SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyyMMddhhmmss");
		date = bank.getInDate() + bank.getInTime(); // 拼接日期
		try {
			depositReturn.setTransTime(simpleDateFormat.parse(date));//缴费时间
		} catch (ParseException e) {
			e.printStackTrace();
		}// 时间
		depositReturn.setInAcct(bank.getInAcct());
		depositReturn.setBid(false);	// 默认为未投标
		depositReturn.setAddWord(bank.getAddWord());
		
    	depositReturnService.saveOrUpdate(depositReturn);
    }
    
    
	//查询履约保证金：，取出并保存到depositeReturn表中
    @Scheduled(cron = "${cron.bank.retrieve.depositeExecute}")
    public void retrieveBankDataOfExecute() throws Exception{
    	if(!bankEnable.equals("1")){
    		System.out.println("禁止bank retrieve!");
    		return;
    	}
    	System.out.println("QuartzBank执行的任务调度");
    	
		String date ="";
    	SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyyMMddhhmmss");
    	List<AuctionInfo>  projs=auctionService.findAllByStatus(null, null, getExecuteProjectStatus());//查询待中标之后的项目
		for (AuctionInfo pi : projs) {
			if( pi.getExecuteBankAuthCode()==null || pi.getExecuteBankAuthCode().equals("")){
				continue;   //不符合条件，不需要写值，直接跳过
			}
			Date nowTime=new Date();
			if(pi.getEndTime()!=null//
					&&nowTime.getTime()>=StringUtil.addOneDayDate(pi.getEndTime(), 3).getTime()){//项目结束时间+3天 不查询
				continue;
			}
			
			List<Bank> bankreturnList = depositExecuteService.getDepositList(pi.getId());
			
			//修改履约保证金状态
			if(bankreturnList!=null&&bankreturnList.size()>=2){
				if(pi.getStatusExecutePromiss()!=2&&pi.getStatusExecutePromiss()!=3){
					pi.setStatusExecutePromiss(1);//已交
					this.auctionService.saveOrUpdate(pi);
				}
			}
			
			for (Bank bank : bankreturnList) {
				if(bank.getInName()==null || bank.getInName().equals("")){
    				continue;  //不符合条件，不需要写值，直接跳过
    			}
    			RegisterUser rUser = null;
				List<RegisterUser> lst =registerUserService.getSameAccountUser(bank.getInAcct());
				if(lst.size()>0){
                   rUser=lst.get(0);
				}
				if (rUser==null){//系统无此账号
			        if(bank.getInName()!=null&&bank.getInAmount()!=null){
			        	//未查到相关数据，系统记录与银行数据不一致，记录系统日志，并忽略这条记录
				        LogInfo log1= new LogInfo("从银行返回的数据中查找不到用户信息"+bank.getInName()+"【"+bank.getInAmount()+"】","","",
				        		String.valueOf(LogType.ADD.ordinal()), "QuartZAll.class", "");
				        LogUtil.addLog(log1);
				        //如果没有发现这个用户，调用系统外用户
	    				this.saveOutDeposit(bank, pi,Constant.DEPOSIT_TYPE_EXECUTE);
			        }
					
					continue;
				}
				if(rUser!=null && !bank.getInAcct().equals(rUser.getAccount())){//系统账号的银行卡号和bank的卡号不一致
					//未查到相关数据，系统记录与银行数据不一致，记录系统日志，并忽略这条记录
			        LogInfo log1= new LogInfo("从银行返回的数据中的银行卡号和注册账号卡号不一致： "+bank.getInName(),"","",
			        		String.valueOf(LogType.ADD.ordinal()), "QuartZAll.class", "");
			        LogUtil.addLog(log1);
    				//如果发现这个用户的系统账号的银行卡号和bank的卡号不一致，调用系统外用户
    				this.saveOutDeposit(bank, pi,Constant.DEPOSIT_TYPE_EXECUTE);
			        
					continue;
				}
				//查询系统外信息中是否查询到此数据
				updateOutDepositReturn(bank,pi,rUser,Constant.DEPOSIT_TYPE_EXECUTE);
				
				ComposeIdPK composeIdPK = new ComposeIdPK();
    			composeIdPK.setCompanyName(bank.getInName());
    			composeIdPK.setProjectId(pi.getId());
    			composeIdPK.setDepositType(Constant.DEPOSIT_TYPE_EXECUTE);
    			composeIdPK.setHstSeqNum(bank.getHstSeqNum());//交易流水号
    			//DepositReturn depositReturn = depositReturnService.get(DepositReturn.class, composeIdPK);
				DepositReturn depositReturn = depositReturnService.getDepositListByHstSeqNum(bank.getHstSeqNum());
				//如果数据库里面取出的保证金对象主键中的单位名称与银行返回的InName 不一致，则删除现有的保证金对象，重新写入保证金对象
				if(depositReturn != null&&!depositReturn.getPk().getCompanyName().equals(bank.getInName())){
                      depositReturn=depositReturnService.updateDepositReturnPK(depositReturn,composeIdPK);
				}
    			if (depositReturn==null ){
    				depositReturn = new DepositReturn();
    				depositReturn.setBankName(rUser.getBank());
    				depositReturn.setProjectType(pi.getCatagoryName());
    				depositReturn.setPk(composeIdPK);
    				depositReturn.setBidderId(rUser.getId());
    				depositReturn.setBiaoDuanNo(pi.getId());// 标段号
    				depositReturn.setProjectName(pi.getAuctionName());
    				depositReturn.setReturnOpt(0);// 默认为未提交申请
    				depositReturn.setStatus(0);// 默认为未退还
    				depositReturn.setProjectCode(pi.getCode());
    				depositReturn.setTnAmount(new BigDecimal(bank.getInAmount()));// 金额
    				date = bank.getInDate() + bank.getInTime(); // 拼接日期
    				depositReturn.setTransTime(simpleDateFormat.parse(date));// 时间
    				depositReturn.setInAcct(bank.getInAcct());
    				this.projectBidService.saveOrUpdate(depositReturn);
    			}
			}
		}
    }
    
    //查询退还的履约保证金：，取出并保存到depositeReturn表中
    @Scheduled(cron = "${cron.bank.retrieve.depositeReturnExecute}")
    public void retrieveBankDataOfReturnExecute(){
    	if(!bankEnable.equals("1")){
    		System.out.println("禁止bank retrieve!");
    		return;
    	}
    	
    	System.out.println("QuartzBank执行的任务调度");
    	
    	//调接口文件 获取已退回保证金用户并保存到deposieReturn
    	
    	AuctionInfo pj= new AuctionInfo();
    	pj.setReturnDepositFlag(false);	
    	
    	List<AuctionInfo>  projs=auctionService.findAllByStatus(null, null, getExecuteReturnProjectStatus());//查询待中标之后的项目
    	for(AuctionInfo projectInfo:projs){
    		if( projectInfo.getBankAuthCode()==null || projectInfo.getBankAuthCode().equals("")){
    			continue;   //不符合条件，不需要写值，直接跳过
    		}
    		
    		List<Bank> banks =this.depositExecuteService.searchReturnDepositByProj(projectInfo.getId(),projectInfo.getAuctionName());
    		if (banks==null || banks.size()==0){
    			continue;   //不符合条件，不需要写值，直接跳过
    		}
    		
    		for(Bank b:banks){
    			
    			if(b.getInName()==null || b.getInName().equals("")){
    				continue;  //不符合条件，不需要写值，直接跳过
    			}
    			//保存已退还的保证金
    			ComposeIdPK composeIdPK = new ComposeIdPK();
    			composeIdPK.setCompanyName(b.getInName());
    			composeIdPK.setProjectId(projectInfo.getId());
    			composeIdPK.setDepositType(Constant.DEPOSIT_TYPE_EXECUTE);   
    			composeIdPK.setHstSeqNum(b.getHstSeqNum());//交易流水号
    			//DepositReturn _dr = depositReturnService.get(DepositReturn.class, composeIdPK);
				DepositReturn _dr = depositReturnService.getDepositListByHstSeqNum(b.getHstSeqNum());
				//如果数据库里面取出的保证金对象主键中的单位名称与银行返回的InName 不一致，则删除现有的保证金对象，重新写入保证金对象
				if(_dr != null&&!_dr.getPk().getCompanyName().equals(b.getInName())){
                      _dr=depositReturnService.updateDepositReturnPK(_dr,composeIdPK);
				}
    			if (_dr==null ){
    				_dr = new DepositReturn();
    			}
//    			DepositReturn _dr =new DepositReturn();
    			
    			List<RegisterUser> lst= registerUserService.getSameAccountUser(b.getInAcct());
    			RegisterUser reg = null;
    			if(lst.size()>0&&lst.get(0)!=null){
    				 reg=lst.get(0);
    				_dr.setBidderId(reg.getId());
    			}else{//如果没有发现这个用户，调用系统外用户
    				this.saveOutReturn(b, projectInfo,Constant.DEPOSIT_TYPE_EXECUTE);
    				continue;
    			}
    			
    			_dr.setPk(composeIdPK);
    			_dr.setInAcct(b.getInAcct());
    			_dr.setTnAmount(new BigDecimal(b.getInAmount()));
        		_dr.setBiaoDuanNo(projectInfo.getId());
        		_dr.setUpdateTime(new Date());
        		_dr.setProjectCode(projectInfo.getCode());
        		_dr.setProjectType(projectInfo.getCatagoryName());

        		
        		//银行接口result:  1: 初始化接收      2: 退款失败           3: 退款成功
        		//修改：0：不成功， 1：成功，TODO:结合AddWord来判断细节如退款失败
        		switch(b.getResult()){
        		case "0":   //
        			_dr.setStatus(Constant.DEPOSIT_UN_RETURN);
        			break;
//        		case "2":   //2: 退款失败
//        			_dr.setStatus(Constant.FAILE_RETURN);
//        			break;
        		case "1":
        			_dr.setStatus(Constant.DEPOSIT_RETURN);
        			String date ="";
        	    	SimpleDateFormat simpleDateFormat= new SimpleDateFormat("yyyyMMddhhmmss");
        			date = b.getInDate() + b.getInTime(); // 拼接日期
        			try {
        				// 退款时间
            			_dr.setReturnTime(simpleDateFormat.parse(date));
        			} catch (ParseException e) {
        				e.printStackTrace();
        			}
        			
        			//修改履约保证金状态
					projectInfo.setStatusExecutePromiss(2);//已退
					this.auctionService.saveOrUpdate(projectInfo);
        			
        			break;
        		}
        		
        		_dr.setAddWord(b.getAddWord());
        		
        		depositReturnService.saveOrUpdate(_dr);
    		}
    	}
    	
    }
    
//    /**
//     * 待开标的状态(投标保证金查询)
//     * @return
//     */
//    private String getBidProjectStatus() {
//		return	ProjectStatus.WAIT_SHOT.toString()//待开标
//				+","+ProjectStatus.SHOTING.toString()//评标开始
//				+","+ProjectStatus.AUDITED_BEGIN.toString()//初审开始
//				+","+ProjectStatus.AUDITED_END.toString()//初审结束
//				+","+ProjectStatus.COMPAT_SHOW_PAUSE.toString();//竞价公示暂停
//	}

	 /**
     * 待开标的状态(投标保证金查询)
     * @return
     */
    private String getBidProjectStatus() {
		return	ProjectStatusAuction.AUCTION_SHOW.toString()
				+","+ProjectStatusAuction.EVALUATE.toString()
				+","+ProjectStatusAuction.SHOW_PAUSE.toString()
				+","+ProjectStatusAuction.PAUSE_AUDIT.toString()
				+","+ProjectStatusAuction.SHOW_QUESTION_NOPASS.toString()
				+","+ProjectStatusAuction.SHOW_QUESTION_HANDLE.toString()
				+","+ProjectStatusAuction.AUCTIONING.toString();
	}
    
    /**
     * 中标之后的状态(履约保证金查询)
     * @return
     */
//    private String getExecuteProjectStatus() {
//		return	ProjectStatus.WAIT_SHOT.toString()  //待开标7
//				+","+ProjectStatus.SHOTING.toString() //评标开始8
//				+","+ProjectStatus.AUDITED_BEGIN.toString()//初审开始，上传专家后改成初审51
//				+","+ProjectStatus.AUDITED_END.toString() //初审结束
//				+","+ProjectStatus.WAIT_WIN_SURE.toString() //中标待确认91
//				+","+ProjectStatus.WAIT_FAIL_SURE.toString()//WAIT_FAIL_SURE
//				+","+ProjectStatus.SHOWING.toString()
//				+","+ProjectStatus.SHOWING_FLOW.toString()
//				+","+ProjectStatus.SHOW_OVER.toString()
//				+","+ProjectStatus.ALERT.toString()
//				+","+ProjectStatus.WAIT_CONTACT_AUDIT.toString()
//				+","+ProjectStatus.CONTACT_AUDIT_NOT_PASS.toString()
//				+","+ProjectStatus.SHOW_CONTACT.toString()
//				+","+ProjectStatus.SHOW_CONTACT_OVER.toString()
//				+","+ProjectStatus.WAIT_RECEIVING.toString()
//				+","+ProjectStatus.RECEIVING_UNAUTH.toString()
//				+","+ProjectStatus.RECEIVING_SHOWING.toString()
//				+","+ProjectStatus.WAIT_PROJ_OVER_SURE.toString()
//				+","+ProjectStatus.PROJ_OVER.toString()
//				+","+ProjectStatus.FLOW_DOWN.toString()
//				+","+ProjectStatus.CANCEL.toString()
//				+","+ProjectStatus.COMPAT_SHOW_PAUSE.toString()
//				+","+ProjectStatus.HIT_SHOW_PAUSE.toString()
//				+","+ProjectStatus.STATUS_LOCK.toString();
//	}

	 /**
     * 中标之后的状态(履约保证金查询)
     * @return
     */
    private String getExecuteProjectStatus() {
		return	ProjectStatusAuction.AUCTION_SHOW.toString()
				+","+ProjectStatusAuction.EVALUATE.toString()
				+","+ProjectStatusAuction.SHOW_PAUSE.toString()
				+","+ProjectStatusAuction.PAUSE_AUDIT.toString()
				+","+ProjectStatusAuction.SHOW_QUESTION_NOPASS.toString()
				+","+ProjectStatusAuction.SHOW_QUESTION_HANDLE.toString()
				+","+ProjectStatusAuction.AUCTIONING.toString()
				+","+ProjectStatusAuction.OWNER_CONFIRM.toString()
				+","+ProjectStatusAuction.CENTER_CONFIRM.toString()
				+","+ProjectStatusAuction.FLOW_SHOW.toString()
				+","+ProjectStatusAuction.HIT_SHOW.toString()
				+","+ProjectStatusAuction.SHOW_CONTRACT_OVER.toString()
				+","+ProjectStatusAuction.PUBLISH_OVER.toString()
				+","+ProjectStatusAuction.AUDIT_CONTRACT.toString()
				+","+ProjectStatusAuction.CONTRACT_AUDIT_NOPASS.toString()
				+","+ProjectStatusAuction.SHOW_CONTRACT.toString()
				+","+ProjectStatusAuction.LOCK_CONTRACT.toString()
				+","+ProjectStatusAuction.UP_CONTACT.toString()
				+","+ProjectStatusAuction.PAUSE_HIT.toString()
				+","+ProjectStatusAuction.EVALUATE_HIT.toString()
				+","+ProjectStatusAuction.PAUSE_HIT_AUDIT.toString()
				+","+ProjectStatusAuction.PAUSE_HIT_NOPASS.toString()
				+","+ProjectStatusAuction.HIT_QUESTION_HANDLE.toString()
				+","+ProjectStatusAuction.END.toString()
				+","+ProjectStatusAuction.FLOW.toString()
				+","+ProjectStatusAuction.TERMINAL.toString();
	}








    
    /**
     * 中标之后的状态(履约保证金退款查询)
     * @return
     */
//    private String getExecuteReturnProjectStatus() {
//    	return	ProjectStatus.WAIT_PROJ_OVER_SURE.toString()//结项确认(等待项目管理员)141
//				+","+ProjectStatus.PROJ_OVER.toString() //项目结束15
//				+","+ProjectStatus.FLOW_DOWN.toString()//流标16
//				+","+ProjectStatus.CANCEL.toString()//撤销状态24
//				+","+ProjectStatus.COMPAT_SHOW_PAUSE.toString()//竞价公示暂停 29
//				+","+ProjectStatus.HIT_SHOW_PAUSE.toString()//中标公示暂停30
//				+","+ProjectStatus.STATUS_LOCK.toString();//锁定88
//	}

  /**
     * 中标之后的状态(履约保证金退款查询)
     * @return
     */
    private String getExecuteReturnProjectStatus() {
    	return	ProjectStatusAuction.END.toString()
				+","+ProjectStatusAuction.FLOW.toString()
				+","+ProjectStatusAuction.TERMINAL.toString();
//				+","+ProjectStatusAuction.COMPAT_SHOW_PAUSE.toString()
//				+","+ProjectStatusAuction.HIT_SHOW_PAUSE.toString()
//				+","+ProjectStatusAuction.STATUS_LOCK.toString();
	}

    
}
